home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_StringIO.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  3.2 KB  |  114 lines

  1. # Tests StringIO and cStringIO
  2.  
  3. import unittest
  4. import StringIO
  5. import cStringIO
  6. import types
  7. import test_support
  8.  
  9.  
  10. class TestGenericStringIO(unittest.TestCase):
  11.     # use a class variable MODULE to define which module is being tested
  12.  
  13.     # Line of data to test as string
  14.     _line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!'
  15.  
  16.     # Constructor to use for the test data (._line is passed to this
  17.     # constructor)
  18.     constructor = str
  19.  
  20.     def setUp(self):
  21.         self._line = self.constructor(self._line)
  22.         self._lines = self.constructor((self._line + '\n') * 5)
  23.         self._fp = self.MODULE.StringIO(self._lines)
  24.  
  25.     def test_reads(self):
  26.         eq = self.assertEqual
  27.         eq(self._fp.read(10), self._line[:10])
  28.         eq(self._fp.readline(), self._line[10:] + '\n')
  29.         eq(len(self._fp.readlines(60)), 2)
  30.  
  31.     def test_writes(self):
  32.         f = self.MODULE.StringIO()
  33.         f.write(self._line[:6])
  34.         f.seek(3)
  35.         f.write(self._line[20:26])
  36.         f.write(self._line[52])
  37.         self.assertEqual(f.getvalue(), 'abcuvwxyz!')
  38.  
  39.     def test_writelines(self):
  40.         f = self.MODULE.StringIO()
  41.         f.writelines([self._line[0], self._line[1], self._line[2]])
  42.         f.seek(0)
  43.         self.assertEqual(f.getvalue(), 'abc')
  44.  
  45.     def test_truncate(self):
  46.         eq = self.assertEqual
  47.         f = self.MODULE.StringIO()
  48.         f.write(self._lines)
  49.         f.seek(10)
  50.         f.truncate()
  51.         eq(f.getvalue(), 'abcdefghij')
  52.         f.seek(0)
  53.         f.truncate(5)
  54.         eq(f.getvalue(), 'abcde')
  55.         f.close()
  56.         self.assertRaises(ValueError, f.write, 'frobnitz')
  57.  
  58.     def test_iterator(self):
  59.         eq = self.assertEqual
  60.         unless = self.failUnless
  61.         it = iter(self._fp)
  62.         # Does this object support the iteration protocol?
  63.         unless(hasattr(it, '__iter__'))
  64.         unless(hasattr(it, 'next'))
  65.         i = 0
  66.         for line in self._fp:
  67.             eq(line, self._line + '\n')
  68.             i += 1
  69.         eq(i, 5)
  70.  
  71. class TestStringIO(TestGenericStringIO):
  72.     MODULE = StringIO
  73.  
  74.     if test_support.have_unicode:
  75.         def test_unicode(self):
  76.  
  77.             # The StringIO module also supports concatenating Unicode
  78.             # snippets to larger Unicode strings. This is tested by this
  79.             # method. Note that cStringIO does not support this extension.
  80.  
  81.             f = self.MODULE.StringIO()
  82.             f.write(self._line[:6])
  83.             f.seek(3)
  84.             f.write(unicode(self._line[20:26]))
  85.             f.write(unicode(self._line[52]))
  86.             s = f.getvalue()
  87.             self.assertEqual(s, unicode('abcuvwxyz!'))
  88.             self.assertEqual(type(s), types.UnicodeType)
  89.  
  90. class TestcStringIO(TestGenericStringIO):
  91.     MODULE = cStringIO
  92.  
  93. import sys
  94. if sys.platform.startswith('java'):
  95.     # Jython doesn't have a buffer object, so we just do a useless
  96.     # fake of the buffer tests.
  97.     buffer = str
  98.  
  99. class TestBufferStringIO(TestStringIO):
  100.     constructor = buffer
  101.  
  102. class TestBuffercStringIO(TestcStringIO):
  103.     constructor = buffer
  104.  
  105.  
  106. def test_main():
  107.     test_support.run_unittest(TestStringIO)
  108.     test_support.run_unittest(TestcStringIO)
  109.     test_support.run_unittest(TestBufferStringIO)
  110.     test_support.run_unittest(TestBuffercStringIO)
  111.  
  112. if __name__ == '__main__':
  113.     test_main()
  114.